//这个文件专门用于处理options表的数据操作
//实现所有分类数据相关操作
var conn = require('../utils/myconn')

//新增导航菜单项
exports.addMenu = (obj,callback)=>{
    let sql = `select value from options where id = 9`
    conn.query(sql,(err,result) =>{
        if(err){
            callback(err)
        }else{
            let jsonStr = result[0].value
            let arr = JSON.parse(jsonStr)
            arr.push(obj)
            let finalStr = JSON.stringify(arr)
            let sql = `update options set value = ? where id = 9`
            conn.query(sql,[finalStr],(err1)=>{
                if(err1){
                    callback(err1)
                }else{
                    callback(null)
                }
            })
        }
    })
}


//获取网站默认设置
exports.getSettings = (callback)=>{
    var sql = `select value from options where id < 9`
    conn.query(sql,(err,results)=>{
        if(err){
            callback(err)
        }else{
            callback(null,results)
        }
    })
}

exports.updataSettings = (obj,callback)=>{
    var cnt = 0
    for(let key in obj){
        let sql = 'update `options` set value=? where `key`= ?'
        conn.query(sql,[obj[key],key],(err,result)=>{
            if(err){
                callback(err)
                return
            }else{
                cnt ++
                if(cnt == 5){
                    callback(null)
                }
            }
        })
    }
}